<template>
  <div>
    <!-- 搜索结果页-头部导航 -->
    <div class="search-result-container">
      <!-- 点击实现后退效果 -->
      <van-nav-bar title="搜索结果" left-arrow @click-left="$router.go(-1)" fixed />
    </div>
    <!-- 搜索结果==文章列表 -->
    <div>
      <van-list
        v-model="loading"
        :finished="finished"
        finished-text="没有更多了"
        @load="onLoad"
        :immediate-check="false"
      >
        <article-item
          v-for="obj in searchResultList"
          :key="obj.art_id"
          :obj="obj"
          :is-show="false"
          @click.native="$router.push(`/article_detail?art_id=${obj.art_id}`)"
        ></article-item>
      </van-list>
    </div>
  </div>
</template>

<script>
import { getSearchReultListAPI } from '@/api'
import ArticleItem from '@/components/ArticleItem.vue'
export default {
  name: 'SearchResult',
  components: {
    ArticleItem
  },
  data () {
    return {
      params: {
        page: 1,
        per_page: 10,
        q: '' // 关键词
      },
      searchResultList: [], // 搜索结果列表
      loading: false,
      finished: false
    }
  },
  created () {
    this.params.q = this.$route.params.keywords
    this.getSearchReultListFn()
  },
  methods: {
    async getSearchReultListFn () {
      const res = await getSearchReultListAPI(this.params)
      // console.log(res)
      // this.searchResultList = res.data.data.results
      this.searchResultList = [...this.searchResultList, ...res.data.data.results]
      this.loading = false
      if (res.data.data.results.length === 0) {
        this.finished = true
      }
    },
    // 加载事件
    onLoad () {
      this.params.page++
      this.getSearchReultListFn()
    }
  }
}
</script>

<style lang="less" scoped>
.search-result-container {
  padding-top: 46px;
}
</style>
